package cn.xz.mytimer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.NumberPicker
import androidx.databinding.DataBindingUtil
import cn.xz.mytimer.databinding.ActivitySettingBinding

class SettingActivity : AppCompatActivity() {

    private lateinit var mDataBind: ActivitySettingBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBind = DataBindingUtil.setContentView(this, R.layout.activity_setting)
        initData()
        initEvent()
    }

    private fun initData(){
        //是否显示间隔动画
        showAni()
        //是否显示年月日
        showYear()
        //是否显示秒数
        showMin()
        //是否显示星期几
        showWeek()
        //开启AI
        showAI()
        //设置间隔时间
        showAniTime()
        //是否开启闹钟
        showNZ()
        //闹钟内容
        showNZC()
        //设置选择器
        setPick()
    }

    private fun initEvent(){
        mDataBind.tvFinish.setOnClickListener {
            finish()
        }
        mDataBind.llcAni.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.SHOW_ANI,!SessionUtils.getBoolean(SessionUtils.SHOW_ANI,true))
            showAni()
        }
        mDataBind.llcYear.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.SHOW_YEAR,!SessionUtils.getBoolean(SessionUtils.SHOW_YEAR,true))
            showYear()
        }
        mDataBind.llcMin.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.SHOW_S,!SessionUtils.getBoolean(SessionUtils.SHOW_S,true))
            showMin()
        }
        mDataBind.llcWeek.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.SHOW_WEEK,!SessionUtils.getBoolean(SessionUtils.SHOW_WEEK,true))
            showWeek()
        }
        mDataBind.llcNl.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.SHOW_NZ,!SessionUtils.getBoolean(SessionUtils.SHOW_NZ,false))
            showNZ()
        }
        mDataBind.llcCxNz.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.NZ_CF,!SessionUtils.getBoolean(SessionUtils.NZ_CF,false))
            showNLCF()
        }
        mDataBind.llcAi.setOnClickListener {
            SessionUtils.commitBoolean(SessionUtils.ZN_AI,!SessionUtils.getBoolean(SessionUtils.ZN_AI,false))
            showAI()
        }
        mDataBind.tvNlTime.setOnClickListener {
            mDataBind.llcNlTime.visibility = View.VISIBLE
        }

        mDataBind.etNzC.addTextChangedListener(object: TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                SessionUtils.commitString(SessionUtils.MY_NZ_CONTENT,mDataBind.etNzC.text.toString())
            }

        })
        mDataBind.tvNlButtonTime.setOnClickListener {
            val s1 = mDataBind.npNz1.value
            val s2 = mDataBind.npNz2.value
            val f1 = mDataBind.npNz3.value
            val f2 = mDataBind.npNz4.value
            SessionUtils.commitString(SessionUtils.MY_TIMER,"$s1$s2$f1$f2")
            nlTime()
            mDataBind.llcNlTime.visibility = View.GONE
        }
        mDataBind.tvAniTime.setOnClickListener {
            //显示选择器
            mDataBind.npTime.visibility = View.VISIBLE
            mDataBind.tvNpTime.visibility = View.VISIBLE
            mDataBind.npTime.value = SessionUtils.getInt(SessionUtils.SHOW_ANI_TIME,20)
        }
        mDataBind.tvNpTime.setOnClickListener {
            val mTime = mDataBind.npTime.value
            SessionUtils.commitInt(SessionUtils.SHOW_ANI_TIME,mTime)
            mDataBind.npTime.visibility = View.GONE
            mDataBind.tvNpTime.visibility = View.GONE
            showAniTime()
        }
        //闹铃时间选择器单独拿出来
        initNL()
    }

    private fun showAni(){
        val showANI = SessionUtils.getBoolean(SessionUtils.SHOW_ANI,true)
        if (showANI){
            mDataBind.tvAni.text = "是"
            mDataBind.tvAniTime.visibility = View.VISIBLE
        }else{
            mDataBind.tvAni.text = "否"
            mDataBind.tvAniTime.visibility = View.GONE
            mDataBind.npTime.visibility = View.GONE
            mDataBind.tvNpTime.visibility = View.GONE
        }
    }

    private fun showYear(){
        val showY = SessionUtils.getBoolean(SessionUtils.SHOW_YEAR,true)
        var s = "是"
        if (!showY){
            s = "否"
        }
        mDataBind.tvYear.text = s

    }

    private fun showMin(){
        val showMin = SessionUtils.getBoolean(SessionUtils.SHOW_S,true)
        var s = "是"
        if (!showMin){
             s = "否"
        }
        mDataBind.tvMin.text = s
    }

    private fun showWeek(){
        val showWeek = SessionUtils.getBoolean(SessionUtils.SHOW_WEEK,true)
        var s = "是"
        if (!showWeek){
            s= "否"
        }
        mDataBind.tvWeek.text = s
    }

    private fun showAI(){
        val showAI = SessionUtils.getBoolean(SessionUtils.ZN_AI,false)
        var s = "是"
        if (!showAI){
            s= "否"
        }
        mDataBind.tvAi.text = s
    }

    //闹铃是否重复
    private fun showNLCF(){
        //是否重复闹铃
        val mC = SessionUtils.getBoolean(SessionUtils.NZ_CF,false)
        var mS = "否"
        if (mC){
            mS = "是"
        }
        mDataBind.tvCxNz.text = mS
    }

    private fun showAniTime(){
        val mT = SessionUtils.getInt(SessionUtils.SHOW_ANI_TIME,20)
        mDataBind.tvAniTime.text = "间隔时间: ${mT}S"
    }

    private fun showNZ(){
        val mN = SessionUtils.getBoolean(SessionUtils.SHOW_NZ,false)
        if (mN){
            mDataBind.tvNl.text = "是"
            mDataBind.llcCxNz.visibility = View.VISIBLE
            mDataBind.tvNlTime.visibility = View.VISIBLE
            mDataBind.etNzC.visibility = View.VISIBLE
            //重复？
            showNLCF()
            //闹铃时间
            nlTime()
        }else{
            mDataBind.tvNl.text = "否"
            mDataBind.llcCxNz.visibility = View.GONE
            mDataBind.tvNlTime.visibility = View.GONE
            mDataBind.etNzC.visibility = View.GONE
            mDataBind.llcNlTime.visibility = View.GONE
        }
    }

    private fun showNZC(){
        val c = SessionUtils.getString(SessionUtils.MY_NZ_CONTENT,"")
        mDataBind.etNzC.setText(c)
    }

    //闹铃时间
    private fun nlTime(){
        val split = SessionUtils.getString(SessionUtils.MY_TIMER,"0000").split("")
        var t = ""
        split.forEachIndexed { index, s ->
            if (s.isNotEmpty()) {
                when(index){
                    1->{
                        mDataBind.npNz1.value = s.toInt()
                        t += s
                        if (s.toInt() == 2){
                            mDataBind.npNz2.maxValue = 3
                        }
                    }
                    2->{
                        mDataBind.npNz2.value = s.toInt()
                        t += "$s:"
                    }
                    3->{
                        mDataBind.npNz3.value = s.toInt()
                        t += s
                    }
                    4->{
                        mDataBind.npNz4.value = s.toInt()
                        t += s
                    }
                }
            }
        }
        mDataBind.tvNlTime.text = t
    }

    private fun setPick(){
        //动画间隔选择器
        mDataBind.npTime.minValue = 10
        mDataBind.npTime.maxValue = 50
        mDataBind.npTime.value = 20
        mDataBind.npTime.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        //闹铃时间选择器
        mDataBind.npNz1.minValue = 0
        mDataBind.npNz2.minValue = 0
        mDataBind.npNz3.minValue = 0
        mDataBind.npNz4.minValue = 0

        mDataBind.npNz1.maxValue = 2
        mDataBind.npNz2.maxValue = 9
        mDataBind.npNz3.maxValue = 5
        mDataBind.npNz4.maxValue = 9

        nlTime()
        mDataBind.npNz1.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        mDataBind.npNz2.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        mDataBind.npNz3.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        mDataBind.npNz4.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS

    }

    private fun initNL(){
        mDataBind.npNz1.setOnValueChangedListener { picker, oldVal, newVal ->
            if (newVal == 2){
                mDataBind.npNz2.maxValue = 3
            }else{
                mDataBind.npNz2.maxValue = 9
            }
        }
    }

}